<?php

namespace App\Http\Controllers\Back;

use App\Http\Controllers\Controller;
use App\Model\Enterprise;
use App\Model\EnterpriseStaff;
use App\Model\Operators;
use App\Model\Salesmen;
use App\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
use Swagger\Annotations as SWG;

/**
 * Class AuthController
 * @package App\Http\Controllers\Back
 */
class EnterpriseController extends Controller
{
    public function list(Request $request,Enterprise $enterprise)
    {

        $where = [];
        // 1城市合伙人2运营3业务
        if ($request->user()->type == 1) {
            $where['place_partner_id'] = $request->user()->id;
        }
        if ($request->user()->type == 2) {
            $where['operator_id'] = $request->user()->id;
        }
        if ($request->user()->type == 3) {
            $where['salesman_id'] = $request->user()->id;
        }

        $limit = request('limit', 15);

        $o = $enterprise->where($where)->paginate($limit)->toArray();

        return $this->renderJson($o);
    }

    // 获取城市、运营、业务员下面的企业列表
    public function sub(Request $request){
        $phone = $request->get('phone');
        $user = User::where('phone', $phone)->first();

        $limit = request('limit', 15);
        $e = '';
        if ($user->type == 1){
            $e = Enterprise::where("place_partner_id", $user->id)->paginate($limit)->toArray();
        }
        if ($user->type == 2){
            $e = Enterprise::where("operator_id", $user->id)->paginate($limit)->toArray();
        }
        if ($user->type == 3){
            $e = Enterprise::where("salesman_id", $user->id)->paginate($limit)->toArray();
        }
        return $this->renderJson($e);
    }

    // 获取企业下面的员工个数
    public function count_staff(Request $request){
        $enterprise_id = $request->get('enterprise_id');
        $r = EnterpriseStaff::where('enterprise_id', $enterprise_id)->count();
        return $this->renderJson(['count' => $r]);
    }
    // 获取企业下面的会员个数
    public function count_user(Request $request){
        $enterprise_id = $request->get('enterprise_id');
        $r = User::where('enterprise_user_id', $enterprise_id)->count();
        return $this->renderJson(['count' => $r]);
    }

    /**
     * @SWG\Get(
     *     path="/back/enterprise/under",
     *     tags={"后台"},
     *     summary="下辖企业",
     *     description="下辖企业",
     *     produces={"application/json"},
     *     @SWG\Parameter(ref="#/parameters/auth"),
     *     @SWG\Parameter(name="name", in="query", type="string", description="账号"),
     *     @SWG\Parameter(name="status", in="query", type="integer", description="状态"),
     *     @SWG\Parameter(name="is_ident", in="query", type="integer", description="认证"),
     *     @SWG\Parameter(name="overed_at", in="query", type="integer", description="过期时间"),
     *     @SWG\Parameter(name="type", in="query", type="integer", description="类型，1管理中心,2运营商3服务商"),
     *     @SWG\Response(
     *          response="200",
     *          description="下辖企业",
     *          @SWG\Schema(
     *              type="array",
     *              @SWG\Items(
     *                  @SWG\Property(property="username", type="string", description="姓名"),
     *                  @SWG\Property(property="phone", type="string", description="电话"),
     *                  @SWG\Property(property="name", type="string", description="企业"),
     *                  @SWG\Property(property="staff_count", type="integer", description="员工数量"),
     *                  @SWG\Property(property="user_count", type="integer", description="会员数量"),
     *                  @SWG\Property(property="province", type="string", description="地区"),
     *                  @SWG\Property(property="overed_at", type="string", format="date", description="过期时间"),
     *                  @SWG\Property(property="is_ident", type="integer", format="date", description="认证状态 0 未认证 1认证中 2已认证 3失败"),
     *                  @SWG\Property(property="status", type="integer", format="date", description="状态 0 封禁 1正常 "),
     *              )
     *          )
     *      )
     * )
     */
    public function under()
    {
        $model = Enterprise::from('enterprise as e')
            ->select('s.name as username', 's.phone', 'e.name', 'sc.count as staff_count', 'uc.count as user_count','p.name as province', 'e.overed_at', 'e.is_ident', 'u.status')
            ->leftJoin('enterprise_users as u', 'u.enterprise_id', '=', 'e.id')
            ->leftJoin('enterprise_staff as s', 's.id', '=', 'u.staff_id')
            //员工数量
            ->leftJoin(DB::raw('(select count(*) as count, enterprise_id from enterprise_staff group by enterprise_id) as sc'), 'sc.enterprise_id', '=', 'e.id')
            //会员数量
            ->leftJoin(DB::raw('(select count(*) as count, enterprise_user_id from users group by enterprise_user_id) as uc'), 'uc.enterprise_user_id', '=', 'e.id')
            ->leftJoin('places as p', 'p.id', '=', 'e.province_id')
            ->when(\request('name'), function ($query, $value) {
                $query->where('e.name', 'like', '%'.$value.'%')
                    ->orWhere('s.name', 'like', '%'.$value.'%')
                    ->orWhere('s.phone', 'like', '%'.$value.'%');
            })->when(\request('status'), function ($query, $value) {
                $query->where('u.status', $value);
            })->when(\request('is_ident'), function ($query, $value) {
                $query->where('e.is_ident', $value);
            })->when(\request('overed_at'), function ($query, $value) {
                $query->where('e.overed_at', '<', $value);
            });
        $type = \request('type');
        if ($type) {
            switch ($type) {
                case 1:
                    $id = Operators::where('place_partner_id', Auth::id())->get()->pluck('id');
                    $model->whereIn('e.operator_id', $id);
                break;
                case 2:
                    $id = Salesmen::where('place_partner_id', Auth::id())->get()->pluck('id');
                    $model->whereIn('e.salesman_id', $id);
            }
        }else {
            // 1城市合伙人2运营3业务
            if (Auth::user()->type == 1) {
                $model->where('e.place_partner_id', Auth::id());
            }
            if (Auth::user()->type == 2) {
                $model->where('e.operator_id', Auth::id());
            }
            if (Auth::user()->type == 3) {
                $model->where('e.salesman_id', Auth::id());
            }
        }
        $list = $model->paginate(\request('limit', 10));
        return $this->renderJson($list);
    }

}